﻿using System;
using System.Collections.Generic;
using System.Text;
using TTClassLibrary;
using TTServer.Messages;
using libCommonClass;
using TTServer.Controller;
using libAsymmetricEncryption;
using libUtilities;

namespace TTServer.MessageProcessor
{
    public class AdminAuthentificationRequestMessageProcessor : IMessageProcessor
    {
        public bool Process(libCommonClass.TTMessage message, libInterface.IClientInfoProvider clientInfoProvider)
        {
            if (message is AdminAuthentificationRequestMessage)
            {
                AdminAuthentificationRequestMessage msg = (AdminAuthentificationRequestMessage)message;

                byte[] encryptedPassword = msg.encryptedPassword;

                byte[] decryptedPassword = RSA_Algorithm.rsaDecryptWithPrivateKey(
                    RSA_KeyManager.getInstance().privateParameters, encryptedPassword);

                string strPassword = System.Text.Encoding.Unicode.GetString(decryptedPassword);

                AdminAuthentificationResponseMessage response = null;

                if (Global.admistratorPassword.Equals(strPassword) == true)
                {
                    clientInfoProvider.Authority = AuthorityConstant.ADMIN;

                    clientInfoProvider.TemporaryKey = OtherUtils.GenerateTemporaryKey();

                    byte[] dataToBeEncrypted = System.Text.Encoding.Unicode.GetBytes(clientInfoProvider.TemporaryKey);

                    byte[] encrypteddata = RSA_Algorithm.rsaEncryptWithPublicKey(
                        clientInfoProvider.ClientPublicKey, dataToBeEncrypted);

                    response =
                    new AdminAuthentificationResponseMessage(true, encrypteddata);
                }
                else if ("77777".Equals(strPassword) == true)
                {
                    //For test
                    String key = OtherUtils.GenerateTemporaryKey();

                    clientInfoProvider.Authority = AuthorityConstant.MEMBER;
                    clientInfoProvider.TemporaryKey = key;

                    NMSAuthentificationResponseMessage res1 = new NMSAuthentificationResponseMessage(
                            NMSAuthentificationResponseMessage.TYPE_SUCCESSFUL,
                            key);

                    clientInfoProvider.AddToMessageQueue(res1);
                }
                else if ("88888".Equals(strPassword) == true)
                {
                    //For test

                    clientInfoProvider.Authority = AuthorityConstant.KEY_MEMBER;
                    clientInfoProvider.TemporaryKey = OtherUtils.GenerateTemporaryKey();

                    byte[] dataToBeEncrypted = System.Text.Encoding.Unicode.GetBytes(clientInfoProvider.TemporaryKey);

                    byte[] encrypteddata = RSA_Algorithm.rsaEncryptWithPublicKey(
                        clientInfoProvider.ClientPublicKey, dataToBeEncrypted);

                    KeyMemberAuthentificationResponseMessage res2 = new KeyMemberAuthentificationResponseMessage(1, encrypteddata);
                    AuthenManagement.getInstance().isKeyMemberAuthenticated = true;

                    clientInfoProvider.AddToMessageQueue(res2);
                }
                else
                {
                    response =
                    new AdminAuthentificationResponseMessage(false, new byte[1]);
                }

                if (response != null)
                {
                    clientInfoProvider.AddToMessageQueue(response);
                }

                return true;
            }

            return false;
        }
    }
}
